// pages/cart/cart.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartLists: [{
        id: 1,
        image: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2836579300,2130896302&fm=15&gp=0.jpg',
        info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
        weight: '1斤正负0.1斤',
        price: '15.99',
        cartnum: 1,
        checked: true
      },
      {
        id: 2,
        image: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2836579300,2130896302&fm=15&gp=0.jpg',
        info: '可比可美味3连贯薯片105/灌3灌/组番茄味十原滋味十烧烤味',
        weight: '1斤正负0.1斤',
        price: '15.99',
        cartnum: 2,
        checked: false
      }
    ],
    edit: true,
    allcheck: false,
    allnum: 0,
    buyNum: 0,
    red: false,
    totalPrice: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let allnum = wx.getStorageSync('allnum') || this.data.allnum
    let cartLists = wx.getStorageSync('cartLists') || this.data.cartLists
    console.log(wx.getStorageSync('cartLists'))
    this.setData({
      cartLists
    })
    let buyNum = 0
    let totalPrice = 0
    for (let i = 0; i < this.data.cartLists.length; i++) {
      if (this.data.cartLists[i].checked) {
        console.log(this.data.cartLists[i].price, this.data.cartLists[i].cartnum)
        totalPrice += this.data.cartLists[i].price * this.data.cartLists[i].cartnum
        buyNum++
        this.setData({
          red: true
        })
      }
    }
    this.setData({
      buyNum,
      totalPrice
    })
    console.log(allnum)
    let num = allnum + ''
    wx.setTabBarBadge({
      index: 2,
      text: num
    });
  },
  edit() {
    this.setData({
      edit: !this.data.edit
    })
  },
  change(e) {
    const that = this
    let {
      index
    } = e.currentTarget.dataset;
    let {
      cartLists
    } = this.data;
    let oldCheckStatus = cartLists[index].checked;
    cartLists[index].checked = !oldCheckStatus;
    this.setData({
      cartLists
    })
    let buyNum = 0
    let totalPrice = 0
    for (let i = 0; i < this.data.cartLists.length; i++) {
      if (this.data.cartLists[i].checked) {
        buyNum++,
        totalPrice += this.data.cartLists[i].price * this.data.cartLists[i].cartnum
      }
    }
    this.setData({
      totalPrice
    })
    if (buyNum > 0) {
      this.setData({
        red: true
      })
    } else {
      this.setData({
        red: false,
      })
    }
    this.setData({
      buyNum
    })
    let allcheck = that.data.allcheck
    for (let i = 0; i < cartLists.length; i++) {
      var count = 0;
      if (cartLists[i].checked == false) {
        allcheck = false
      }
      for (var j = 0; j < cartLists.length; j++) {
        if (cartLists[j].checked == true) {
          count += 1;
        }
      }
      if (count == cartLists.length) {
        allcheck = true;
      }
    }
    this.setData({
      allcheck
    })
  },
  allchange(e) {
    this.setData({
      allcheck: !this.data.allcheck
    })
    const that = this
    let cartLists = this.data.cartLists
    console.log(cartLists)
    for (let i = 0; i < cartLists.length; i++) {
      cartLists[i].checked = that.data.allcheck
    }
    console.log(cartLists)
    this.setData({
      cartLists
    })
    let buyNum = 0
    let totalPrice = 0
    for (let i = 0; i < this.data.cartLists.length; i++) {
      if (this.data.cartLists[i].checked) {
        buyNum++,
        totalPrice += this.data.cartLists[i].price * this.data.cartLists[i].cartnum
      }
    }
    this.setData({
      totalPrice
    })
    if (buyNum > 0) {
      this.setData({
        red: true
      })
    } else {
      this.setData({
        red: false
      })
    }
    this.setData({
      buyNum
    })
  },
  add(e) {
    console.log(e)
    let cartLists = [...this.data.cartLists]
    cartLists[e.currentTarget.dataset.index].cartnum = cartLists[e.currentTarget.dataset.index].cartnum + 1
    this.setData({
      cartLists
    })
    let totalPrice = 0
    for (let i = 0; i < this.data.cartLists.length; i++) {
      if (this.data.cartLists[i].checked) {
        totalPrice += this.data.cartLists[i].price * this.data.cartLists[i].cartnum
      }
    }
    this.setData({
      totalPrice
    })
    wx.setStorage({
      data: cartLists,
      key: 'cartLists',
    })
    let allnum = this.data.allnum
    for (let i = 0; i < cartLists.length; i++) {
      allnum += cartLists[i].cartnum
    }
    console.log(allnum)
    wx.setStorage({
      data: allnum,
      key: 'allnum',
    })
    let num = allnum + ''
    wx.setTabBarBadge({
      index: 2,
      text: num
    });
  },
  decrease(e) {
    const that = this
    console.log(e)
    let cartLists = [...this.data.cartLists]
    cartLists[e.currentTarget.dataset.index].cartnum = cartLists[e.currentTarget.dataset.index].cartnum - 1
    this.setData({
      cartLists
    })
    let totalPrice = 0
            for (let i = 0; i < that.data.cartLists.length; i++) {
              if (that.data.cartLists[i].checked) {
                totalPrice += that.data.cartLists[i].price * that.data.cartLists[i].cartnum
              }
            }
            that.setData({
              totalPrice
            })
    if (cartLists[e.currentTarget.dataset.index].cartnum < 1) {
      cartLists[e.currentTarget.dataset.index].cartnum = 1
      that.setData({
        cartLists
      })
      let totalPrice = 0
      for (let i = 0; i < that.data.cartLists.length; i++) {
        if (that.data.cartLists[i].checked) {
          totalPrice += that.data.cartLists[i].price * that.data.cartLists[i].cartnum
        }
      }
      that.setData({
        totalPrice
      })
      wx.showModal({
        title: '确认删除',
        content: '您确定要删除商品吗',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            cartLists.splice(e.currentTarget.dataset.index, 1)
            console.log(cartLists)
            let allnum = wx.getStorageSync('allnum')
            allnum = allnum - 1
            that.setData({
              cartLists,
              allnum
            })
            let totalPrice = 0
            for (let i = 0; i < that.data.cartLists.length; i++) {
              if (that.data.cartLists[i].checked) {
                totalPrice += that.data.cartLists[i].price * that.data.cartLists[i].cartnum
              }
            }
            that.setData({
              totalPrice
            })
            wx.setStorage({
              data: cartLists,
              key: 'cartLists',
            })
            wx.setStorage({
              data: allnum,
              key: 'allnum',
            })
            let num = allnum + ''
            wx.setTabBarBadge({
              index: 2,
              text: num
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
            cartLists[e.currentTarget.dataset.index].cartnum = 1
            that.setData({
              cartLists
            })
            let totalPrice = 0
            for (let i = 0; i < that.data.cartLists.length; i++) {
              if (that.data.cartLists[i].checked) {
                totalPrice += that.data.cartLists[i].price * that.data.cartLists[i].cartnum
              }
            }
            that.setData({
              totalPrice
            })
          }
        }
      })
    }
    wx.setStorage({
      data: cartLists,
      key: 'cartLists',
    })
    let allnum = this.data.allnum
    cartLists = [...this.data.cartLists]
    console.log(allnum, cartLists)
    for (let i = 0; i < cartLists.length; i++) {
      allnum += cartLists[i].cartnum
    }
    console.log(allnum)
    wx.setStorage({
      data: allnum,
      key: 'allnum',
    })
    let num = allnum + ''
    wx.setTabBarBadge({
      index: 2,
      text: num
    });
  },
  buy(){
     wx.navigateTo({
       url: '/pages/buy/buy',
     })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})